﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace DataLayer
{
    public class Database
    {
        private OleDbConnection cnn;
        private OleDbDataAdapter da;
        private OleDbCommand cmd;
        ReadConnect constring = new ReadConnect(false, Application.StartupPath + @"\chuoiketnoi.ini");

        public Database()
        {
            try
            {
                string connectring = constring.docchuoiketnoi();
                cnn = new OleDbConnection(connectring);
                cmd = cnn.CreateCommand();
            }
            catch (SqlException sqle)
            {
                MessageBox.Show(sqle.Message);
            }
        }
        public DataTable laydulieu(string sql,CommandType ct,params OleDbParameter[] param )
        {
            DataTable dt = new DataTable();
            cnn.Open();
            cmd.Parameters.Clear();
            cmd.CommandText = sql;
            cmd.CommandType = ct;
            cmd.Parameters.Clear();
            if (param != null)
            {
                foreach (OleDbParameter p in param)
                    cmd.Parameters.Add(p);
            }
            try
            {
                da = new OleDbDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dt = ds.Tables[0];
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cnn.Close();
            }
            return dt;

        }
        public object layso(string sql, CommandType ct)
        {
            object o=null;
            cnn.Open();
            cmd.CommandText = sql;
            cmd.CommandType = ct;

            try
            {
                o = cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cnn.Close();
            }
            return o;
        }


        public bool MyExecuteNonQuery(string strSql, CommandType ct, params OleDbParameter[] param)
        {
            bool f = false;
            cnn.Open();
            cmd.Parameters.Clear();
            cmd.CommandText = strSql;
            cmd.CommandType = ct;
            if (param != null)
            {
                foreach (OleDbParameter p in param)
                    cmd.Parameters.Add(p);
            }
            try
            {
                cmd.ExecuteNonQuery();
                f = true;
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cnn.Close();
            }
            return f;
        }
    }
}
